﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Drawing

Namespace Controls

    <DefaultProperty("Location"), _
    ToolboxData("<{0}:CurrentConditions runat=server></{0}:CurrentConditions>"), _
    ToolboxBitmap(GetType(ResourceFinder), "CurrentConditions.png")> _
    Public Class CurrentConditions
        Inherits WebControl

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property Location() As String
            Get
                Dim s As String = CStr(ViewState("Location"))
                If s Is Nothing Then
                    Return String.Empty
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("Location") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property PartnerID() As String
            Get
                Dim s As String = CStr(ViewState("PartnerID"))
                If s Is Nothing Then
                    Return String.Empty
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("PartnerID") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property Key() As String
            Get
                Dim s As String = CStr(ViewState("Key"))
                If s Is Nothing Then
                    Return String.Empty
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("Key") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property Units() As Weather.Data.Units
            Get
                Dim s As String = CStr(ViewState("Units"))
                If s Is Nothing Then
                    Return Weather.Data.Units.Standard
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As Weather.Data.Units)
                ViewState("Units") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property IconFolder() As String
            Get
                Dim s As String = CStr(ViewState("IconFolder"))
                If s Is Nothing Then
                    Return String.Empty
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("IconFolder") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property WeatherIconType() As Weather.Data.IconTypes
            Get
                Dim s As String = CStr(ViewState("WeatherIconType"))
                If s Is Nothing Then
                    Return Weather.Data.IconTypes.Png
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As Weather.Data.IconTypes)
                ViewState("WeatherIconType") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property MoonIconType() As Weather.Data.IconTypes
            Get
                Dim s As String = CStr(ViewState("MoonIconType"))
                If s Is Nothing Then
                    Return Weather.Data.IconTypes.Png
                Else
                    Return s
                End If
            End Get
            Set(ByVal Value As Weather.Data.IconTypes)
                ViewState("MoonIconType") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property ShowDetails() As Boolean
            Get
                If IsNothing(ViewState("ShowDetails")) Then
                    Return True
                Else
                    Return CBool(ViewState("ShowDetails"))
                End If
            End Get
            Set(ByVal Value As Boolean)
                ViewState("ShowDetails") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property ShowMoonInfo() As Boolean
            Get
                If IsNothing(ViewState("ShowMoonInfo")) Then
                    Return True
                Else
                    Return CBool(ViewState("ShowMoonInfo"))
                End If
            End Get
            Set(ByVal Value As Boolean)
                ViewState("ShowMoonInfo") = Value
            End Set
        End Property

        <Bindable(True), Category("Misc"), DefaultValue(""), Localizable(False)> Property ShowWeatherLogo() As Boolean
            Get
                If IsNothing(ViewState("ShowWeatherLogo")) Then
                    Return True
                Else
                    Return CBool(ViewState("ShowWeatherLogo"))
                End If
            End Get
            Set(ByVal Value As Boolean)
                ViewState("ShowWeatherLogo") = Value
            End Set
        End Property

        Public Overrides Sub RenderBeginTag(ByVal writer As System.Web.UI.HtmlTextWriter)
            AddAttributesToRender(writer)
            writer.RenderBeginTag(HtmlTextWriterTag.Div)
        End Sub

        Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
            With writer
                'Data.
                Dim o As Object = Weather.Data.Provider.GetCurrentConditions(Me.Location, Me.PartnerID, Me.Key, Me.Units)
                Dim cc As Weather.Data.CurrentConditions = Nothing

                'Check errors.
                If TypeOf o Is Weather.Data.ServiceError Then
                    Dim err As Weather.Data.ServiceError = DirectCast(o, Weather.Data.ServiceError)
                    .Write(String.Format("<div class=""weather-error"">"))
                    .Write(err.Description)
                    .Write(String.Format("</div>"))
                    Exit Sub
                Else
                    cc = DirectCast(o, Weather.Data.CurrentConditions)
                End If

                'Icon folders.
                If Me.IconFolder.EndsWith("/") = False Then Me.IconFolder = String.Format("{0}/", Me.IconFolder)
                Dim if1 As String = String.Format("{0}Weather/", Me.IconFolder)
                Dim if2 As String = String.Format("{0}Moon/", Me.IconFolder)
                Dim if3 As String = String.Format("{0}Other/", Me.IconFolder)

                'Location.
                .Write(String.Format("<div class=""weather-location"">"))
                .Write(cc.Location)
                .Write(String.Format("</div>"))

                'Icon.
                .Write(String.Format("<div class=""weather-icon"">"))
                Select Case Me.WeatherIconType
                    Case Data.IconTypes.Png
                        .Write(String.Format("<img src=""{0}{1}.png"" alt=""{2}""/>", if1, cc.Icon, cc.Description))
                    Case Data.IconTypes.Gif
                        .Write(String.Format("<img src=""{0}{1}.gif"" alt=""{2}""/>", if1, cc.Icon, cc.Description))
                End Select
                .Write(String.Format("</div>"))

                'Main content.
                .Write(String.Format("<div class=""weather-main-content"">"))
                .Write(String.Format("<div class=""weather-temperature"">{0}&deg;{1}</div>", cc.Temperature, cc.UnitTemperature))
                .Write(String.Format("<div class=""weather-description"">{0}</div>", My.Resources.Weather.ResourceManager.GetString(cc.Description)))
                .Write(String.Format("</div>"))

                'Separator.
                .Write(String.Format("<div class=""weather-separator"">"))
                .Write(String.Format("</div>"))

                'Presure icon.
                Dim pi As String = String.Empty
                Select Case cc.PressureDescription
                    Case My.Resources.Weather.PressureSteady
                        pi = String.Format("<img src=""{0}00.png"" alt=""{1}"" title=""{1}"" class=""weather-presure-icon""/>", if3, My.Resources.Weather.ResourceManager.GetString(cc.PressureDescription))
                    Case My.Resources.Weather.PressureFalling
                        pi = String.Format("<img src=""{0}01.png"" alt=""{1}"" title=""{1}"" class=""weather-presure-icon""/>", if3, My.Resources.Weather.ResourceManager.GetString(cc.PressureDescription))
                    Case My.Resources.Weather.PressureRising
                        pi = String.Format("<img src=""{0}02.png"" alt=""{1}"" title=""{1}"" class=""weather-presure-icon""/>", if3, My.Resources.Weather.ResourceManager.GetString(cc.PressureDescription))
                End Select

                'UV Index icon.
                Dim uvi As String = String.Empty
                Select Case cc.UVIndex
                    Case 0, 1, 2
                        uvi = "03"
                    Case 3, 4, 5
                        uvi = "04"
                    Case 6, 7
                        uvi = "05"
                    Case 8, 9, 10
                        uvi = "06"
                    Case Else
                        uvi = "07"
                End Select

                uvi = String.Format("<img src=""{0}{1}.png"" alt=""{2}"" title=""{2}"" class=""weather-uvindex-icon""/>", if3, uvi, My.Resources.Weather.ResourceManager.GetString(cc.UVDescription))

                'Details.
                If Me.ShowDetails = True Then
                    Dim tr As String = "<tr class=""{3}""><td class=""weather-detail-label"">{0}:</td><td class=""weather-detail-data"">{1}{2}</td></tr>"
                    .Write(String.Format("<div class=""weather-detail-content"">"))
                    .Write(String.Format("<table cellpadding=""0"" cellspacing=""0"" width=""100%"">"))
                    .Write(String.Format(tr, My.Resources.Weather.RealFeel, cc.RealFealTemperature, String.Format("&deg;{0}", cc.UnitTemperature), "weather-detail-data-row"))
                    .Write(String.Format(tr, My.Resources.Weather.WindSpeed, cc.WindSpeed, String.Format(" {0}", cc.UnitSpeed), "weather-detail-data-altrow"))
                    .Write(String.Format(tr, My.Resources.Weather.WindDirection, String.Format("{0}&deg;", cc.WindDirection), String.Format(" {0}", Data.GetDirectionResource(cc.WindDirectionDescription)), "weather-detail-data-row"))
                    .Write(String.Format(tr, My.Resources.Weather.Presure, pi & FormatNumber(cc.Pressure, 1), String.Format(" {0}", cc.UnitPresure), "weather-detail-data-altrow"))
                    .Write(String.Format(tr, My.Resources.Weather.RelativeHumidity, cc.RelativeHumidity, "&#37;", "weather-detail-data-row"))
                    .Write(String.Format(tr, My.Resources.Weather.Visiblity, FormatNumber(cc.Visiblity, 1), String.Format(" {0}", cc.UnitDistance), "weather-detail-data-altrow"))
                    .Write(String.Format(tr, My.Resources.Weather.DewPoint, cc.DewPoint, String.Format("&deg;{0}", cc.UnitTemperature), "weather-detail-data-row"))
                    .Write(String.Format(tr, My.Resources.Weather.UVIndex, uvi, cc.UVIndex, "weather-detail-data-altrow"))
                    .Write(String.Format(tr, My.Resources.Weather.Sunrise, FormatDateTime(cc.Sunrise, DateFormat.ShortTime), String.Empty, "weather-detail-data-row"))
                    .Write(String.Format(tr, My.Resources.Weather.Sunset, FormatDateTime(cc.Sunset, DateFormat.ShortTime), String.Empty, "weather-detail-data-altrow"))
                    .Write(String.Format(tr, My.Resources.Weather.Longitude, cc.Longitude, "&deg;", "weather-detail-data-row"))
                    .Write(String.Format(tr, My.Resources.Weather.Latitude, cc.Latitude, "&deg;", "weather-detail-data-altrow"))
                    .Write(String.Format("</table>"))
                    .Write(String.Format("</div>"))
                End If

                'Moon.
                If Me.ShowMoonInfo = True Then
                    .Write(String.Format("<div class=""weather-moon"">"))
                    .Write(String.Format("<table cellpadding=""0"" cellspacing=""0"" width=""100%""><tr>"))
                    Select Case Me.MoonIconType
                        Case Data.IconTypes.Png
                            .Write(String.Format("<td class=""weather-moon-icon""><img src=""{0}{1}.png"" alt=""{2}""/></td>", if2, cc.MoonIcon, cc.MoonDescription))
                        Case Data.IconTypes.Gif
                            .Write(String.Format("<td class=""weather-moon-icon""><img src=""{0}{1}.gif"" alt=""{2}""/></td>", if2, cc.MoonIcon, cc.MoonDescription))
                    End Select
                    .Write(String.Format("<td class=""weather-moon-data"">{0}</td>", My.Resources.Weather.ResourceManager.GetString(cc.MoonDescription)))
                    .Write(String.Format("</tr></table>"))
                    .Write(String.Format("</div>"))
                End If

                'Weather Channel.
                If Me.ShowWeatherLogo = True Then
                    .Write(String.Format("<div class=""weather-channel"">"))
                    .Write(String.Format("<a href=""http://weather.com"" target=""_blank""><img src=""{0}"" alt=""{1}"" style=""border:none;""/></a>", Me.Page.ClientScript.GetWebResourceUrl(GetType(Controls.CurrentConditions), "Manic.Services.Weather.WeatherChannel.png"), My.Resources.Weather.TheWeatherChannel))
                    .Write(String.Format("</div>"))
                End If

                'Separator.
                .Write(String.Format("<div class=""weather-separator"">"))
                .Write(String.Format("</div>"))

                'Update time.
                .Write(String.Format("<div class=""weather-updatetime"">"))
                .Write(Format(cc.LastUpdate, "f"))
                .Write(String.Format("</div>"))
            End With
        End Sub

        Public Overrides Sub RenderEndTag(ByVal writer As System.Web.UI.HtmlTextWriter)
            writer.RenderEndTag()
        End Sub

    End Class

End Namespace